package article

import (
	"admin.supervise.limaochuxing.top/admin/schemas/req"
	"admin.supervise.limaochuxing.top/admin/schemas/resp"
	"admin.supervise.limaochuxing.top/core/request"
	"admin.supervise.limaochuxing.top/core/response"
	"admin.supervise.limaochuxing.top/model/article"

	"gorm.io/gorm"
)

type IArticleCategoryService interface {
	List(page request.PageReq, listReq req.ArticleCategoryListReq) (res response.PageResp, e error)
	Detail(id int) (res resp.ArticleCategoryResp, e error)
	Add(addReq req.ArticleCategoryAddReq) (e error)
	Edit(editReq req.ArticleCategoryEditReq) (e error)
	Del(id int) (e error)
	All() (res []resp.ArticleCategoryResp, e error) // 新增获取所有分类的方法
	Change(id int) (e error) // 修改参数，移除 status
}

//NewArticleCategoryService 初始化
func NewArticleCategoryService(db *gorm.DB) IArticleCategoryService {
	return &articleCategoryService{db: db}
}

//articleCategoryService 文章分类服务实现类
type articleCategoryService struct {
	db *gorm.DB
}

//List 文章分类列表
func (srv articleCategoryService) List(page request.PageReq, listReq req.ArticleCategoryListReq) (res response.PageResp, e error) {
	// 分页信息
	limit := page.PageSize
	offset := page.PageSize * (page.PageNo - 1)
	// 查询
	model := srv.db.Model(&article.ArticleCategory{})
	if listReq.Name != "" {
        model = model.Where("name like ?", "%"+listReq.Name+"%")
    }
    // if listReq.Sort >=0 {
    //     model = model.Where("sort = ?", listReq.Sort)
    // }
    // if listReq.IsShow >=0 {
    //     model = model.Where("is_show = ?", listReq.IsShow)
    // }
	model = model.Where("is_delete = ?", 0)
	// 总数
	var count int64
	err := model.Count(&count).Error
	if e = response.CheckErr(err, "List Count err"); e != nil {
		return
	}
	// 数据
	var objs []article.ArticleCategory
	err = model.Limit(limit).Offset(offset).Order("id desc").Find(&objs).Error
	if e = response.CheckErr(err, "List Find err"); e != nil {
		return
	}
	resps := []resp.ArticleCategoryResp{}
	response.Copy(&resps, objs)
	return response.PageResp{
		PageNo:   page.PageNo,
		PageSize: page.PageSize,
		Count:    count,
		Lists:    resps,
	}, nil
}

//Detail 文章分类详情
func (srv articleCategoryService) Detail(id int) (res resp.ArticleCategoryResp, e error) {
	var obj article.ArticleCategory
	err := srv.db.Where("id = ? AND is_delete = ?", id, 0).Limit(1).First(&obj).Error
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Detail First err"); e != nil {
		return
	}
	response.Copy(&res, obj)
	return
}

//Add 文章分类新增
func (srv articleCategoryService) Add(addReq req.ArticleCategoryAddReq) (e error) {
	var obj article.ArticleCategory
	response.Copy(&obj, addReq)
	err := srv.db.Create(&obj).Error
	e = response.CheckErr(err, "Add Create err")
	return
}

//Edit 文章分类编辑
func (srv articleCategoryService) Edit(editReq req.ArticleCategoryEditReq) (e error) {
	var obj article.ArticleCategory
	err := srv.db.Where("id = ? AND is_delete = ?", editReq.Id, 0).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Edit First err"); e != nil {
		return
	}
	// 更新
	response.Copy(&obj, editReq)
	// 明确指定要更新的字段，确保零值也会被更新
	err = srv.db.Model(&obj).Select("name", "sort", "is_show").Updates(obj).Error
	e = response.CheckErr(err, "Edit Updates err")
	return
}

//Del 文章分类删除
func (srv articleCategoryService) Del(id int) (e error) {
	var obj article.ArticleCategory
	err := srv.db.Where("id = ? AND is_delete = ?", id, 0).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Del First err"); e != nil {
		return
	}
    // 删除
    obj.IsDelete = 1
    err = srv.db.Save(&obj).Error
    e = response.CheckErr(err, "Del Save err")
	return
}

//All 获取所有文章分类（不分页）
func (srv articleCategoryService) All() (res []resp.ArticleCategoryResp, e error) {
	// 查询
	model := srv.db.Model(&article.ArticleCategory{})
	// 只查询未删除且显示的分类
	model = model.Where("is_delete = ? AND is_show = ?", 0, 1)
	
	// 数据
	var objs []article.ArticleCategory
	err := model.Order("sort desc, id desc").Find(&objs).Error
	if e = response.CheckErr(err, "All Find err"); e != nil {
		return
	}
	
	response.Copy(&res, objs)
	return res, nil
}

//Change 切换文章分类显示状态
func (srv articleCategoryService) Change(id int) (e error) {
	var obj article.ArticleCategory
	err := srv.db.Where("id = ? AND is_delete = ?", id, 0).Limit(1).First(&obj).Error
	// 校验
	if e = response.CheckErrDBNotRecord(err, "数据不存在!"); e != nil {
		return
	}
	if e = response.CheckErr(err, "Change First err"); e != nil {
		return
	}
	
	// 更新状态：如果当前是1则变为0，如果是0则变为1
	newStatus := 1 - obj.IsShow
	err = srv.db.Model(&obj).Update("is_show", newStatus).Error
	e = response.CheckErr(err, "Change Update err")
	return
}
